<?php
class Hopdong_Model_Phongban extends Zend_Db_Table{
	
	protected $_name = 'phongban';
	protected $_primary ='id';
	protected $_ids;
	
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
		$quyen_nv = new Zendvn_System_Info();
		$quyen_nhom = $quyen_nv->getGroupInfo('permission');
		$quyen_info = $quyen_nv->getInfo();
		$quyen_phongban = $quyen_nv->getPhongbanInfo();

		if($options['task'] == null) {
			$select = $db	->	select()
							->	from('phongban AS pb',array('id','name', 'parents', 'order'))
							->	order('pb.order ASC')
							->	order('pb.id ASC');
			
			$result = $db	->	fetchAll($select);
		}
		
		if($options['task'] == 'admin-edit'){
			$id = $arrParam['id'];
			$select = $db	->	select()
							->	from('phongban AS pb',array('id','name', 'parents', 'order'))
							->	where('pb.id != ?', $arrParam['id'])
							->	order('pb.order ASC')
							->	order('pb.id ASC');
			
			$result = $db	->	fetchAll($select);
		}
		
		$system = new Zendvn_System_Recursive($result);
		if(count($result) > 0) {
			if($quyen_nhom == 'Limit Access') {
				$result = $system->builArray($quyen_phongban['id'], 2);
				if(count($result) > 0) {
					$tmp = array('id' => $quyen_phongban['id'], 'name' => $quyen_phongban['name'], 'level' => 0, 'parents' => $quyen_phongban['parents'], 'order' => $quyen_phongban['order']);
					array_unshift($result, $tmp);
				} else {
					$result =  array('0' => array('id' => $quyen_phongban['id'], 'name' => $quyen_phongban['name'], 'level' => 0, 'parents' => $quyen_phongban['parents'], 'order' => $quyen_phongban['order']));
				}
			} else {
				$result = $system->builArray();
				$tmp = array('id' => 0, 'name' => '- Lựa chọn -', 'level' => 1, 'parents' => 0, 'order' => 1);
				array_unshift($result, $tmp);
			}
		} else {
			$result = array('0' => array('id' => 0, 'name' => '- Lựa chọn -', 'level' => 1, 'parents' => 0, 'order' => 1));
		}
		
		return $result;
	}
	
	public function getItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db	->	select()
							->	from('phongban AS pb', array('*', "DATE_FORMAT(pb.created,'%d/%m/%Y %H:%i:%s') AS created", "DATE_FORMAT(pb.modified,'%d/%m/%Y %H:%i:%s') AS modified"))
							->	where('pb.id = ?', $arrParam['id']);
			$result = $db	->	fetchRow($select);
		}
		return $result;
	}
	
	public function sortItem($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if($options['task'] == 'admin-sort'){
			if(count($cid) > 0){
				foreach ($cid as $key => $val){
					$data = array('order' => $order[$val]);
					$where = 'id = ' . $val;
					$this->update($data, $where);
				}
			}
		}
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
		$paginator = $arrParam['paginator'];
		$ssFilter  = $arrParam['ssFilter'];
		
		if($options['task'] == 'admin-list' ||$options['task'] == 'admin-excel' ){
			$select = $db	->	select()
							->	from('phongban AS pb',array('id','name','parents','status','order'))
							->	order('pb.order ASC')
							->	order('pb.id ASC');
			
			if(!empty($ssFilter['keywords'])){
				$keywords 	= '%' . $ssFilter['keywords'] . '%';
				$select		->	where('pb.name LIKE ?', $keywords);
			}
			
			$parents = 0;
			if(!empty($ssFilter['parents'])){
				$parents = $ssFilter['parents'];
			}
			
			$result = $db->fetchAll($select);
			$system = new Zendvn_System_Recursive($result);
			$result = $system->builArray($parents);
			
			if(count($result) > 0) {
				foreach ($result AS $key => $val) {
					$select_hd = $db->	select()
									->	from('nam_hopdong AS hd',array("id"))
									->	columns('SUM(hd_dongia) AS hd_dongia')
									->	columns('SUM(hd_thanhtien) AS hd_thanhtien')
									->	columns('SUM(hd_sotiendadong) AS hd_sotiendadong')
									->	columns('SUM(hd_sotienno) AS hd_sotienno')
									->	group('hd.id_phongban')
									->	where('hd.id_phongban = ?', $val['id']);
							
// 							if (!empty($ssFilter['date_nam'])) {
// 								$select_hd->where("DATE_FORMAT(hd.ngayky,'%Y') = '" . $ssFilter['date_nam'] . "'");
// 							} else {
// 								$date = new Zend_Date();
// 								$dateNamnay = $date->toString('YYYY');
// 								$select_hd->where("DATE_FORMAT(hd.ngayky,'%Y') = '" . $dateNamnay . "'");
// 							}
							
// 							if (isset($ssFilter['date_thang'])) {
// 								if ($ssFilter['date_thang'] != 0) {
// 								$select_hd->where("DATE_FORMAT(hd.ngayky,'%m') = ?", $ssFilter['date_thang']);
// 								}
// 							} else {
// 								$date = new Zend_Date();
// 								$dateThangnay = $date->toString('MM');
// 								$select_hd->where("DATE_FORMAT(hd.ngayky,'%m') = ?", $dateThangnay);
// 							}

					
// 							echo $select_hd;die;
// 							echo $ssFilter['date_nam'];
// 							echo $ssFilter['date_thang'];
// 							echo $dateThangnay;
// 							die;
							
					$result_hd = $db->fetchRow($select_hd);
					$result[$key]['doanhthu'] = $result_hd;
// 					echo '<pre>';
// 					print_r($result);
// 					echo '</pre>';
				}
			}
		}
		
		if($options['task'] == 'admin-excel'){
			
			
		}
		
// 		echo '<pre>';
// 		print_r($result);
// 		echo '</pre>';
		
		return $result;
	}
	
	public function saveItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-add'){
			$info 					= new Zendvn_System_Info();
			$created_by 			= $info->getMemberInfo('id');
			
			$row 					= $this->fetchNew();
			
			$row->name 				= stripslashes($arrParam['name']);
			$row->status 			= $arrParam['status'];
			$row->parents 			= $arrParam['parents'];
			$row->order 			= $arrParam['order'];
			$row->created 			= @date("Y-m-d H:i:s");
			$row->created_by		= $created_by;
			$row->content 			= stripslashes($arrParam['content']);

			$row->save();
		}
		
		if($options['task'] == 'admin-edit'){
			$where 					= ' id=' . $arrParam['id'];
			$row 					= $this->fetchRow($where);
			
			$info 					= new Zendvn_System_Info();
			$modified_by 			= $info->getMemberInfo('id');
			
			$row->name 				= stripslashes($arrParam['name']);
			$row->status 			= $arrParam['status'];
			$row->parents 			= $arrParam['parents'];
			$row->order 			= $arrParam['order'];
			$row->modified 			= @date("Y-m-d H:i:s");
			$row->modified_by 		= $modified_by;
			$row->content 			= stripslashes($arrParam['content']);
			
			$row->save();
		}
	}
	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$id 	= $arrParam['id'];
			$select = $db	->	select()
							->	from('phongban AS pb',array('id','name','parents','status','order'));
			$result = $db	->	fetchAll($select);
			$system = new Zendvn_System_Recursive($result);
			$result = $system->builArray($id);
			if(count($result)>0){
				array_unshift($result, array('id'=>$arrParam['id']));
			}else{
				$result[] = array('id' => $arrParam['id']);
			}

			$tmp = '';
			foreach ($result as $key => $val){
				if($key == 0){
					$tmp .= $val['id'];
				}else{
					$tmp .= ',' . $val['id'];
				}
			}
			
			foreach ($result as $key => $val){
				$where = ' id=' . $val['id'];
				$result = $this->delete($where);
			}
		}
		
		if($options['task'] == 'admin-delete-muti'){
			$cid = explode(',', $arrParam['cid']);
			if(!empty($cid) && isset($arrParam['cid'])){
				$db = Zend_Registry::get('connectDb');
				//$db = Zend_Db::factory($adapter, $config);
				$id 		= $arrParam['id'];
				$select 	= $db	->	select()
									->	from('phongban AS pb',array('id','name','parents','status','order'));
				$result 	= $db	->	fetchAll($select);
				$newArray = array();
				foreach ($cid as $key => $val){
					$id = $val;
					$newArray[] = array('id' => $id);
					$system = new Zendvn_System_Recursive($result);
					$tmp = $system->builArray($id);
					foreach ($tmp as $keyTmp => $valTmp){
						$newArray[] = $valTmp;
					}
				}
			}

			if(count($newArray)>0){
				$tmp = '';
				foreach ($newArray as $key => $val){
					if($key == 0){
						$tmp .= $val['id'];
					}else{
						$tmp .= ',' . $val['id'];
					}
				}
				
				foreach ($newArray as $keyNew => $valNew){
					$where = ' id=' . $valNew['id'];
					$result = $this->delete($where);
				}
			}
			
		}
	}
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		if(count($cid) > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$id = implode(',', $cid);
			$data = array('status' => $status);
			$where = 'id IN (' . $id . ')';
			$this->update($data, $where);
		}
		if($arrParam['id'] > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array('status' => $status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
}